package com.vcredit.creditcat.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.Banner;
import com.vcredit.creditcat.persistence.base.BannerMapper;
import com.vcredit.creditcat.vo.BannerVo;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@Service
public class BannerService extends BaseService {

	@Autowired
	private BannerMapper bannerMapper;

	public Page<Banner> findByPage(Criteria criteria) {
		return new Page<Banner>(bannerMapper.findByPageCount(criteria), bannerMapper.findByPage(criteria));
	}

	public List<Banner> find(Banner banner) {
		return bannerMapper.find(banner);
	}

	public Banner findById(Long bannerId) {
		return bannerMapper.findById(bannerId);
	}

	public List<Banner> findAll() {
		return bannerMapper.findAll();
	}

	public void saveBanner(Banner banner) {
		bannerMapper.insert(banner);
	}

	public void save(BannerVo bannerVo) {
		try {
			Banner banner = new Banner();
			BeanUtils.copyProperties(banner, bannerVo);
			bannerMapper.insert(banner);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void update(Banner banner) {
		bannerMapper.update(banner);
	}

	public void removeBanner(String ids) {
		for (String bannerIdString : ids.split(",")) {
			Long bannerId = Long.parseLong(bannerIdString);
			bannerMapper.delete(bannerId);
		}
	}

	public Map<String, Object> upladImage(HttpServletRequest request) throws Exception {
		String fileName = "";
		System.out.println(request.getParameter("handInputIdHold1"));
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, Object> mapRestult = new HashMap<String, Object>();
		try {
			String path = fileImgPathForWin(multipartRequest);
			System.out.println(path);
			// 广告封面
			MultipartFile cover = multipartRequest.getFile("handInputIdHold1");
			// 广告跳转
			MultipartFile jump = multipartRequest.getFile("handInputIdHold2");
			if (cover != null) {
				cover.transferTo(new File(path + cover.getOriginalFilename()));
				fileName += cover.getOriginalFilename() + ";";
			}
			if (jump != null) {
				jump.transferTo(new File(path + jump.getOriginalFilename()));
				fileName += jump.getOriginalFilename();
			}
			mapRestult.put("rescode", "success");
			mapRestult.put("fileName", fileName);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			mapRestult.put("rescode", "faled");
			mapRestult.put("fileName", "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return mapRestult;
	}

}
